import api from '@api/mop_api';
import { toast, err, back, set } from '@utils/wxapi';

Page({
  data: {
    mobile: '',
    password: '',
  },
  onShow() {
    const mobile = wx.getStorageSync('mobile') || '';
    this.setData({ mobile });
  },
  async login() {
    try {
      const { mobile, password } = this.data;
      if (!mobile || !password) {
        toast('请将信息输入完整');
        return;
      }
      const res = await api.moplogin({ mobile, password });
      if (res.respCode === 200) {
        const { token, source } = res;
        const { mchId } = JSON.parse(source);
        set('token', token);
        set('mchId', mchId);
        set('mobile', mobile);
        toast('登录成功');
        back();
      } else {
        toast(res.msg);
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
});
